package com.neusoft.chenhanhua.d0921;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.nio.file.Files;
import java.nio.file.Path;

public class Copy {
	/**
	 * * 	作业
	 * 		1、在API文档中查看IO流相关类
	 * 		2、分别使用字节流和字符流实现，文本文件、图片、视频，doc无图片，doc有图片的复制，
	 * 		3、在作业2的基础上理解字节流与字符的差异	
	 */
	public static void main(String[] args) {
		//t3();
		//t4();
		//t5();
		//t6();
		t8();
	}
	//复制文本文件
	public static void t1() {
		String path="files"+File.separator+"1.txt";
		String path1="files"+File.separator+"3.txt";
		File te=new File(path);
		File tr=new File(path1);
		try {
			Files.copy(te.toPath() ,tr.toPath());
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	//复制照片
	public static void t2() {
		String path="files"+File.separator+"2.jpg";
		String path1="files"+File.separator+"3.jpg";
		File te=new File(path);
		File tr=new File(path1);
		try {
			Path qPath= Files.copy(te.toPath() ,tr.toPath());
			System.out.println(qPath);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	//字节流,文字
	public static void t3() {
		String path="files"+File.separator+"1.txt";
		String path1="files"+File.separator+"2.txt";
		byte[] b = new byte[3];//实际开发中，一般一次读取1KB
		int len = -1;//保存当前读取的长度
		FileInputStream fis = null;
		FileOutputStream fi1=null;
		try {
			fis = new FileInputStream(path);
			fi1 = new FileOutputStream(path1,true);
			while((len = fis.read(b)) != -1) {
				System.out.println(new String(b, 0, len));
				fi1.write(b);
//			System.out.println(new String(b));
			}
		} catch (IOException e) {
			e.printStackTrace();
		}finally {
			if (fis !=null) {
				try {
					fis.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}
	//照片
	public static void t4() {
		String path="C:\\Users\\chenhan\\Desktop\\美照\\手机20191111\\IMG_20191109_162439.jpg";
		String path1="files"+File.separator;
		copy(path, path1);
	}
	//视频
	public static void t5() {
		long q=System.currentTimeMillis();
		String path="C:\\Users\\chenhan\\Desktop\\美照\\手机20191111\\VID_20191025_211802.mp4";
		String path1="files"+File.separator;
		copy(path, path1);
		long q2=System.currentTimeMillis();
		System.out.println("复制需要的时间："+(q2-q));
	}
	//缓冲流,缓冲流就是在io流的外面加大了一层的意思
	public static void t6() {
		long q=System.currentTimeMillis();
		String path="C:\\Users\\chenhan\\Desktop\\美照\\手机20191111\\VID_20191025_211802.mp4";
		String path1="files"+File.separator;
		
		byte[] b = new byte[2];//实际开发中，一般一次读取1KB
		int len = -1;//保存当前读取的长度
		FileInputStream fis = null;
		FileOutputStream fi1=null;
		BufferedInputStream br=null;
		BufferedOutputStream bo=null;
		try {
			fis = new FileInputStream(path);
			//获取拷贝的照片
			File fspath=new File(path);
			br=new BufferedInputStream(fis);
			//获取要拷贝到的路径
			File f1=new File(path1);
			
			//判断要拷贝到的是文件还是文件夹
			if (f1.isDirectory()) {
				path1+=File.separator+fspath.getName();
			}
			fi1 = new FileOutputStream(path1,true);
			bo=new BufferedOutputStream(fi1);
			while((len = br.read(b)) != -1) {
				bo.write(b);
//			System.out.println(new String(b));
			}
		} catch (IOException e) {
			e.printStackTrace();
		}finally {
			if (br !=null) {
				try {
					br.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			if (bo!=null) {
				try {
					bo.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
		long q2=System.currentTimeMillis();
		System.out.println("复制需要的时间："+(q2-q));
		
	}
	
	//这是转换流，是将字节流转换为字符流
	public static void t7(String path,String path1) {
		FileInputStream fis = null;
		FileOutputStream fi1=null;
		InputStreamReader isr=null;
		OutputStreamWriter osw=null;
		try {
			fis = new FileInputStream(path);
			//获取拷贝的照片
			isr=new InputStreamReader(fis,"UTF-8");
			//获取要拷贝到的路径
			fi1 = new FileOutputStream(path1,true);
			osw= new OutputStreamWriter(fi1,"UTF-8");
			char[] b = new char[1024];//实际开发中，一般一次读取1KB
			int len = -1;//保存当前读取的长度
			while((len = isr.read(b)) != -1) {
				osw.write(b, 0, len);
				System.out.println(osw);
		//	System.out.println(new String(b));
			}
		} catch (IOException e) {
			e.printStackTrace();
		}finally {
			if (isr !=null) {
				try {
					isr.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			if (osw !=null) {
				try {
					osw.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
		
	}
      public static void t8() {
    	  String path="files"+File.separator+"1.txt";
  		String path1="files"+File.separator+"3.txt";
  		t7(path, path1);
	}
	public static void copy(String path,String path1) {
		byte[] b = new byte[1024];//实际开发中，一般一次读取1KB
		int len = -1;//保存当前读取的长度
		FileInputStream fis = null;
		FileOutputStream fi1=null;
		try {
			fis = new FileInputStream(path);
			//获取拷贝的照片
			File fspath=new File(path);
			//获取要拷贝到的路径
			File f1=new File(path1);
			//判断要拷贝到的是文件还是文件夹
			if (f1.isDirectory()) {
				path1+=File.separator+fspath.getName();
			}
			fi1 = new FileOutputStream(path1,true);
			while((len = fis.read(b)) != -1) {
				fi1.write(b);
//			System.out.println(new String(b));
			}
		} catch (IOException e) {
			e.printStackTrace();
		}finally {
			if (fis !=null) {
				try {
					fis.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}

}
